package com.example.educate.service.impl;



import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.educate.exception.EducateException;
import com.example.educate.listener.SubjectListListener;
import com.example.educate.listener.SubjectListener;
import com.example.educate.mapper.SubjectMapper;
import com.example.educate.model.vod.Subject;
import com.example.educate.service.SubjectService;
import com.example.educate.vo.vod.SubjectEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
<<<<<<< HEAD
 * @author Xiao
 * @since 2022-09-17
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Autowired
    private SubjectListListener subjectListListener;

    @Override
    public List<Subject> selectSubjectList(Long id) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Subject> subjectList = baseMapper.selectList(wrapper);
        for (Subject subject : subjectList) {
            Long subjectId = subject.getId();
            boolean isChild = this.isChildren(subjectId);
            subject.setHasChildren(isChild);
        }
        return subjectList;
    }


    private boolean isChildren(Long subjectId) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",subjectId);
        Integer count = baseMapper.selectCount(wrapper);
        return count>0;
    }


//@Service
//public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {
//    @Autowired
//    private SubjectListener subjectListener;
//    //父层数据
//    @Override
//    public List<Subject> selectSubjectList(Long id) {
//        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
//        wrapper.eq("parent_id", id);
//        List<Subject> subjects = baseMapper.selectList(wrapper);
//        for (Subject subject : subjects) {
//            Long id1 = subject.getId();
//            Boolean isChildren=this.isChildren(id1);
//            subject.setHasChildren(isChildren);
//        }
//        return subjects;
//    }

    @Override
    public void exportData(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("课程分类", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            List<Subject> subjectList = baseMapper.selectList(null);
            List<SubjectEeVo> subjectEeVoList = new ArrayList<>();
            for (Subject subject : subjectList) {
                SubjectEeVo subjectEeVo = new SubjectEeVo();
//                subjectEeVo.setId(subject.getId());
//                subjectEeVo.setParentId(subject.getParentId());
                BeanUtils.copyProperties(subject,subjectEeVo);
                subjectEeVoList.add(subjectEeVo);
            }
            EasyExcel.write(response.getOutputStream(), SubjectEeVo.class)
//            EasyExcel.write("D:\\atguigi.xlsx", SubjectEeVo.class)
                    .sheet("课程分类")
                    .doWrite(subjectEeVoList);
        }catch (Exception e){
            throw new EducateException(20001,"导出失败");
        }

    }

    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),SubjectEeVo.class,subjectListListener)
                    .sheet()
                    .doRead();
        }catch (Exception e){
            throw new EducateException(20001,"导入失败");
        }
    }
}

//=======
//            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
//            String fileName = URLEncoder.encode("课程分类", "UTF-8");
//            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
//            List<Subject> dictList = baseMapper.selectList(null);
//            List<SubjectEeVo> dictVoList = new ArrayList<>(dictList.size());
//            for(Subject dict : dictList) {
//                SubjectEeVo dictVo = new SubjectEeVo();
//                BeanUtils.copyProperties(dict,dictVo);
//                dictVoList.add(dictVo);
//            }
//            EasyExcel.write(response.getOutputStream(), SubjectEeVo.class).sheet("课程分类").doWrite(dictVoList);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        }
    //课程分类导入
//    @Override
//    public void importData(MultipartFile file) {
//        try {
//            EasyExcel.read(file.getInputStream(),
//                    SubjectEeVo.class,subjectListener).sheet().doRead();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//    private Boolean isChildren(Long id1) {
//        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
//        wrapper.eq("parent_id",id1);
//        Integer integer = baseMapper.selectCount(wrapper);
//        return integer>0;
//    }
//}

